<?php
    session_start();

    include("conexion.php");
    $NumeTorneo = $_POST["NumeTorneo"];
    
    $con = new mysqli($dbhost, $dbuser, $dbpass, $db) or die("Problemas al conectar al Servidor");

    $strSQL = "SELECT RutaLogo, NombEqui, COUNT(*) PJ, SUM(PG) PG, SUM(PP) PP, SUM(PF) PF, SUM(PC) PC, SUM(DIF) DIF, SUM(Ptos) Ptos";
    $strSQL.= " FROM (";
    $strSQL.= "   SELECT COALESCE(RutaLogo, 'imagenes/sin-avatar.png') RutaLogo, NombEqui, PtosEquiLoca PF, PtosEquiVisi PC, PtosEquiLoca - PtosEquiVisi DIF,";
    $strSQL.= "   CASE WHEN PtosEquiLoca > PtosEquiVisi THEN 1 ELSE 0 END AS PG,";
    $strSQL.= "   CASE WHEN PtosEquiLoca > PtosEquiVisi THEN 0 ELSE 1 END AS PP,";
    $strSQL.= "   CASE WHEN PtosEquiLoca > PtosEquiVisi AND PtosEquiLoca > 0 THEN 2 ELSE IF (PtosEquiLoca > 0, 1, 0) END AS Ptos";
    $strSQL.= "   FROM partidos p";
    $strSQL.= "   INNER JOIN equipos e ON p.NumeEquiLoca = e.NumeEqui";
    $strSQL.= "   WHERE p.NumeTorneo = " . $NumeTorneo;
    $strSQL.= "   AND p.NumeEsta IN (0)";
    $strSQL.= "   UNION ALL";
    $strSQL.= "   SELECT COALESCE(RutaLogo, 'imagenes/sin-avatar.png') RutaLogo, NombEqui, PtosEquiVisi PF, PtosEquiLoca PC, PtosEquiVisi - PtosEquiLoca DIF,";
    $strSQL.= "   CASE WHEN PtosEquiVisi > PtosEquiLoca THEN 1 ELSE 0 END AS PG,";
    $strSQL.= "   CASE WHEN PtosEquiVisi > PtosEquiLoca THEN 0 ELSE 1 END AS PP,";
    $strSQL.= "   CASE WHEN PtosEquiVisi > PtosEquiLoca AND PtosEquiVisi > 0 THEN 2 ELSE IF (PtosEquiVisi > 0, 1, 0) END AS Ptos";
    $strSQL.= "   FROM partidos p";
    $strSQL.= "   INNER JOIN equipos e ON p.NumeEquiVisi = e.NumeEqui";
    $strSQL.= "   WHERE p.NumeTorneo = " . $NumeTorneo;
    $strSQL.= "   AND p.NumeEsta IN (0)";
    $strSQL.= "	  AND p.NumeFase = 1";
    $strSQL.= " ) tabla";
    $strSQL.= " GROUP BY RutaLogo, NombEqui";
    $strSQL.= " ORDER BY Ptos DESC, DIF DESC, PF DESC";

    if (!($tabla = $con->query($strSQL))) {
		$strSalida = "Error al realizar la consulta.<br>";
		$strSalida.= $con->error;		    	
    }
    else {
	    $i = 1;
	    $strSalida = "";
	    //Chequeo si hay playoffs
	    $strAux = buscarDato("SELECT COUNT(*) FROM partidos WHERE NumeTorneo = ".$NumeTorneo." AND NumeFase <> 1", $dbhost, $db, $dbuser, $dbpass);
	    
	    if ($strAux > 0) {
	    	$strSalida.= '<a href="verPlayOffs.php?NumeTorneo='.$NumeTorneo.'" class="botonTexto">Play Offs</a>';
	    }
	
	    if ($tabla->num_rows > 0) {
		    $strSalida.= $crlf.'<table class="tabla" id="tablaClasificaciones">';
		    $strSalida.= $crlf.'<tr class="tablaFilaTitulo">';
		    $strSalida.= $crlf.'<th title="Posici&oacute;n">#</th>';
		    $strSalida.= $crlf.'<th>Equipo</th>';
		    $strSalida.= $crlf.'<th title="Partidos Jugados" style="cursor: pointer;" onclick="ordenar(2);">PJ</th>';
		    $strSalida.= $crlf.'<th title="Partidos Ganados" style="cursor: pointer;" onclick="ordenar(3);">PG</th>';
		    $strSalida.= $crlf.'<th title="Partidos Perdidos" style="cursor: pointer;" onclick="ordenar(4);">PP</th>';
		    $strSalida.= $crlf.'<th title="Eficiencia" style="cursor: pointer;" onclick="ordenar(5);">%</th>';
		    $strSalida.= $crlf.'<th title="Puntos a Favor" style="cursor: pointer;" onclick="ordenar(6);">PF</th>';
		    $strSalida.= $crlf.'<th title="Puntos en Contra" style="cursor: pointer;" onclick="ordenar(7);">PC</th>';
		    $strSalida.= $crlf.'<th title="Diferencia de Puntos" style="cursor: pointer;" onclick="ordenar(8);">DIF</th>';
		    $strSalida.= $crlf.'<th title="Puntos Generales" style="cursor: pointer;" onclick="ordenar(9);">Ptos</th>';
		    $strSalida.= $crlf.'</tr>';
		    
		    while ($fila = $tabla->fetch_array()) {
		        if ($i % 2 == 0)
		            $strSalida.= $crlf.'<tr class="tablaFilaAlternativa">';
		        else
		            $strSalida.= $crlf.'<tr class="tablaFilaNormal">';
		        
		        //Posicion
		        $strSalida.= $crlf.'<td style="text-align: right;">'.$i.'</td>';
		        //Equipo
		        $strSalida.= $crlf.'<td id="NombEqui'.$i.'" style="text-align: center;"><img src="'.$fila["RutaLogo"].'" title="'.$fila["NombEqui"].'" style="width: 50px; height: 50px;" /></td>';
		        //PJ
		        $strSalida.= $crlf.'<td style="text-align: right;">'.$fila["PJ"].'</td>';
		        //PG
		        $strSalida.= $crlf.'<td style="text-align: right;">'.$fila["PG"].'</td>';	        
		        //PP
		        $strSalida.= $crlf.'<td style="text-align: right;">'.$fila["PP"].'</td>';
		        //%
		        $strSalida.= $crlf.'<td style="text-align: right;">'.round($fila["PG"]/$fila["PJ"]*100, 0).'%</td>';
		        //PF
		        $strSalida.= $crlf.'<td style="text-align: right;">'.$fila["PF"].'</td>';
		        //PC
		        $strSalida.= $crlf.'<td style="text-align: right;">'.$fila["PC"].'</td>';
		        //DIF
		        $strSalida.= $crlf.'<td style="text-align: right;">'.$fila["DIF"].'</td>';
		        //Ptos
		        $strSalida.= $crlf.'<td style="text-align: right;">'.$fila["Ptos"].'</td>';
		        $strSalida.= $crlf.'</tr>';
		        
		        $i++;
		    }
		    $tabla->free();
			$strSalida.=$crlf.'</table>';
		}
	    else
	    	$strSalida = "El torneo no tiene ning&uacute;n partido disputado.";
    }
    
    $con->close();

    
    echo $strSalida;
?>
